package com.sinodata.bsm.center.web.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.struts2.ServletActionContext;

/**
 * 
 * <p>
 * Description: 导出Excel响应Action父类
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-5-30 AM 11:34:17    liulibo        1.0         To create
 * </p>
 * @since 
 * @see   
 * @param <T>
 */
public abstract class BaseExportAction<T> extends BaseAction<T> {

    /**
     * 
     */
    private static final long serialVersionUID = -8310851070452294627L;

    /**
     * 样式表MAP集合
     */
    private Map<String, HSSFCellStyle> styleMap;

    /**
     * 下载文件存放路径
     */
    protected final static String DOWNLOADFILEPATH = "tmp-files/";

    /**
     * Excel表格
     */
    protected HSSFWorkbook workBook = new HSSFWorkbook();

    /**
     * 标题样式
     */
    public static final String STYLE_HEADING = "STYLE_HEADING";

    /**
     * 隔行样式第二行
     */
    public static final String STYLE_CONTENT = "STYLE_CONTENT";

    /**
     * 默认样式
     */
    public static final String STYLE_DEFAULT = "STYLE_DEFAULT";

    /**
     * 文件名称
     */
    public String fileName;

    /**
     * 构造函数
     */
    public BaseExportAction() {
        initStyle();
    }

    /**
     * 初始化样式
     */
    protected void initStyle() {
        styleMap = new HashMap<String, HSSFCellStyle>();
        // 标题样式
        HSSFCellStyle headingStyle = workBook.createCellStyle();
        headingStyle.setWrapText(true);
        headingStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
        headingStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        headingStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        headingStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        headingStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        headingStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        headingStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        HSSFFont font = workBook.createFont();
        font.setColor(HSSFColor.BLACK.index);
        font.setFontHeightInPoints((short) 14);
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        headingStyle.setFont(font);
        headingStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        styleMap.put(STYLE_HEADING, headingStyle);
        // 隔行样式 第二行样式
        HSSFCellStyle contentStyle = workBook.createCellStyle();
        // 自定义颜色
        HSSFPalette palette = workBook.getCustomPalette();
        palette.setColorAtIndex(HSSFColor.PALE_BLUE.index, (byte) 197, // red
                        (byte) 217, // RGB green
                        (byte) 241 // RGB blue
        );
        palette.setColorAtIndex(HSSFColor.SKY_BLUE.index, (byte) 141, // red
                        (byte) 180, // RGB green
                        (byte) 227 // RGB blue
        );
        contentStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
        contentStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        styleMap.put(STYLE_CONTENT, contentStyle);
        // 默认样式
        HSSFCellStyle defaultStyle = workBook.createCellStyle();
        styleMap.put(STYLE_DEFAULT, defaultStyle);
    }

    /**
     * 获取Excel样式
     * 
     * @param styleType
     * @return
     */
    protected HSSFCellStyle getStyle(String styleType) {
        HSSFCellStyle style = styleMap.get(styleType);
        return style == null ? styleMap.get(STYLE_DEFAULT) : style;
    }

    /**
     * 
     * 
     * @param outputStream
     * @throws IOException
     */
    public void write(OutputStream outputStream) throws IOException {
        workBook.write(outputStream);
    }

    /**
     * 执行导出动作
     * 
     * @return
     * @throws Exception
     */
    public abstract String doExport() throws Exception;

    /**
     * 获取输入流
     * 
     * @return
     * @throws FileNotFoundException
     */
    public InputStream getInputStream() throws FileNotFoundException {
        String path = ServletActionContext.getServletContext().getRealPath("/") + DOWNLOADFILEPATH + fileName;
        InputStream in = new FileInputStream(new File(path));
        return in;
    }

    /**
     * 获取下载文件名称
     * 
     * @return
     */
    public String getDownloadFileName() {
        String downFileName = fileName;
        try {
            downFileName = new String(downFileName.getBytes(), "ISO-8859-1");
        } catch (UnsupportedEncodingException e) {
            logger.error("when get the download file name error occured!" + e);
        }
        return downFileName;
    }

    @Override
    public String delete() throws Exception {
        return null;
    }

    @Override
    public String list() throws Exception {
        return null;
    }

    @Override
    protected void prepareModel() throws Exception {

    }

    @Override
    public String save() throws Exception {
        return null;
    }

    @Override
    public T getModel() {
        return null;
    }

}